package com.jqb.jsonlab.demo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
/**
 * org.json
 */
public class JsonOa {
	
	@Test
	public void test01() throws JSONException {
		/**
		 * JSON对象
		 */
		String jsonText = "{name:'jqb',age:18}";
		
		JSONObject jsonObj = new JSONObject(jsonText);
		
		String name = jsonObj.getString("name");
		int age = jsonObj.getInt("age");
		
		System.out.println("name:"+name+",age:"+age);
		
		/**
		 * JSON数组
		 */
		jsonText = "[{name:'aaa', age:22, sex:'male'}," +
				"{name:'bbb', age:21, sex:'female'}," +
				"{name:'ccc', age:19, sex:'male'}]";
		
		JSONArray jsonArray  = new JSONArray(jsonText);
		
		for(int i=0;i<jsonArray.length();i++){
			
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			
			String n = jsonObject.getString("name");
			int a = jsonObject.getInt("age");
			String s = jsonObject.getString("sex");
			
			System.out.println("name:"+n+",age:"+a+",sex:"+s);
		}
		
	}
}
